Skip to main content

Lesson 2: Control Flow

Let’s dive into Lesson 2: Control Flow, which includes conditional statements, loops, and basic algorithms. This lesson will teach you how to control the flow of a program based on conditions and repetitions while introducing simple algorithmic logic.

1. Conditional Statements

Overview

Conditional statements allow a program to execute specific blocks of code based on certain conditions.

  • if: Executes a block of code if a condition is true.
  • if-else: Provides an alternative block of code if the condition is false.
  • else if: Tests multiple conditions.
  • switch: An alternative to multiple if-else statements, useful for checking specific values.

Code Example: if-else and else if

public class ConditionalExample {
public static void main(String[] args) {
int age = 18;

if (age >= 18) {
System.out.println("You are an adult.");
} else if (age >= 13) {
System.out.println("You are a teenager.");
} else {
System.out.println("You are a child.");
}
}
}

Expected Output:

You are an adult.

Code Example: switch

public class SwitchExample {
public static void main(String[] args) {
int day = 3; // Representing Wednesday

switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
}
}

Expected Output:

Wednesday

2. Loops

Overview

Loops allow a block of code to repeat multiple times.

  • for: Repeats a block of code for a known number of iterations.
  • while: Repeats a block of code while a condition is true.
  • do-while: Executes a block of code at least once before checking the condition.

Code Example: for Loop

public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
}
}

Expected Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

Code Example: while Loop

public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;

while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
}
}

Expected Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Code Example: do-while Loop

public class DoWhileExample {
public static void main(String[] args) {
int i = 1;

do {
System.out.println("Number: " + i);
i++;
} while (i <= 5);
}
}

Expected Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

3. Basic Algorithms

Algorithm 1: Find the Largest of Three Numbers

public class LargestNumber {
public static void main(String[] args) {
int a = 10, b = 20, c = 15;

if (a >= b && a >= c) {
System.out.println("The largest number is: " + a);
} else if (b >= a && b >= c) {
System.out.println("The largest number is: " + b);
} else {
System.out.println("The largest number is: " + c);
}
}
}

Expected Output:

The largest number is: 20

Algorithm 2: Sum of Numbers from 1 to N

import java.util.Scanner;

public class SumToN {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a positive number: ");
int n = scanner.nextInt();

int sum = 0;

for (int i = 1; i <= n; i++) {
sum += i;
}

System.out.println("The sum of numbers from 1 to " + n + " is: " + sum);
}
}

Sample Console Interaction:

Enter a positive number: 5
The sum of numbers from 1 to 5 is: 15

Algorithm 3: Multiplication Table

import java.util.Scanner;

public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");
int number = scanner.nextInt();

for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number * i));
}
}
}

Sample Console Interaction:

Enter a number: 3
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30